﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using System.Web.SessionState;
using DailyReport.Helpers;

namespace DailyReport
{
    public class Global : System.Web.HttpApplication
    {
        /// <summary>每日定时（8点）发送todo项超时邮件</summary>
        private System.Timers.Timer _timerHour;

        protected void Application_Start(object sender, EventArgs e)
        {
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

            _timerHour = new Timer(1000 * 60 * 60);
            _timerHour.Elapsed += TimerHourElapsed;
            _timerHour.Start();
        }

        private void TimerHourElapsed(object sender, ElapsedEventArgs e)
        {
            DateTime now = DateTime.Now;
            // 每日定时（8点）发送todo项超时邮件
            if (now.Hour == 8)
            {
                DrEmailHelper.SendTodoRemind();
            }
            // 每周五下午提醒编写下周计划
            if (now.DayOfWeek == DayOfWeek.Friday && (now.Hour == 15 || now.Hour == 17))
            {
                DrEmailHelper.SendNextWeekPlanRemind();
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            if (_timerHour != null)
            {
                _timerHour.Stop();
                _timerHour.Dispose();
                _timerHour = null;
            }
        }
    }
}